package com.free.bsf.eureka.client.limitrate;
import lombok.*;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

public class LimitRateConfigProvider{
    private List<LimitRateConfig> configs = new ArrayList<>();
    private ConcurrentHashMap<String,LimitRateConfig> urlCaches = new ConcurrentHashMap<String,LimitRateConfig>();

    public LimitRateConfigProvider(){
        loadConfigs();
    }

    private void loadConfigs(){
       val configs = LimitRateProperties.getLimitRateConfigs0();
       for(val config:configs.values()){
          // val c = LimitRateConfig.create(config);
          this.configs.add(config);
       }
    }

    public LimitRateConfig parse(String className,String methodName){
        String url = className+"."+methodName;
        if(!urlCaches.contains(url)){
            LimitRateConfig findConfig=null;
            for(val c:this.configs){
                if(c.matchMethod(url))
                {
                    findConfig = c;
                    break;
                }
            }
            if(findConfig!=null) {
                urlCaches.put(url,findConfig);
            }
        }
        val config = urlCaches.get(url);
        if(config==null){
           return null;
        }
        else{
            return config;
        }
    }
}

